import { reactive } from 'vue';

// Central store for application state
export const store = reactive({
  // User information
  user: {
    id: null,
    username: '',
    isLoggedIn: false
  },
  
  // Connected users in the metaverse
  connectedUsers: [],
  
  // Selected car information
  selectedCar: null,
  
  // Selected dealer information
  selectedDealer: null,
  
  // Chat messages
  chatMessages: [],
  
  // System messages
  systemMessages: [],
  
  // Current position in the metaverse
  position: {
    x: 0,
    y: 0,
    z: 0
  },
  
  // Methods to update the store
  setUser(userData) {
    this.user = { ...userData, isLoggedIn: true };
  },
  
  updateConnectedUsers(users) {
    this.connectedUsers = users;
  },
  
  selectCar(car) {
    this.selectedCar = car;
  },
  
  selectDealer(dealer) {
    this.selectedDealer = dealer;
  },
  
  addChatMessage(message) {
    this.chatMessages.push(message);
    
    // Limit chat history to last 50 messages
    if (this.chatMessages.length > 50) {
      this.chatMessages.shift();
    }
  },
  
  updatePosition(x, y, z) {
    this.position = { x, y, z };
  },
  
  // Add a connected user
  addConnectedUser(user) {
    if (!this.connectedUsers.find(u => u.id === user.id)) {
      this.connectedUsers.push(user);
    }
  },
  
  // Remove a connected user
  removeConnectedUser(userId) {
    this.connectedUsers = this.connectedUsers.filter(user => user.id !== userId);
  },
  
  // Show a system message (disappears after 3 seconds)
  showSystemMessage(message) {
    const systemMessage = {
      id: Date.now(),
      message,
      timestamp: new Date().toISOString()
    };
    
    this.systemMessages.push(systemMessage);
    
    // Auto-remove after 3 seconds
    setTimeout(() => {
      this.removeSystemMessage(systemMessage.id);
    }, 3000);
    
    return systemMessage.id;
  },
  
  // Remove a system message
  removeSystemMessage(id) {
    this.systemMessages = this.systemMessages.filter(msg => msg.id !== id);
  }
}); 